package com.huanying.goodjob.callcenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;
import com.huanying.framework.adminuser.Adminuser;
import com.huanying.framework.adminuser.AdminuserService;
import com.huanying.framework.user.User;

import com.huanying.goodjob.job.JobService;
import com.huanying.goodjob.job.Jobitem_User;

@Controller
public class CallcenterController extends BaseController{
	private static Logger logger = Logger.getLogger(CallcenterController.class);
	@Autowired
	private CallcenterService callService;
	@Autowired
	private AdminuserService userService;
	@Autowired
	private JobService jobService;
	//后台展示任务
	@RequestMapping(value = "/show_add_jobitem_call.do")
	public String show_add_jobitem_call(String user_id,String page_num, Model model)
	{
		Adminuser user=(Adminuser) this.getSession().getAttribute("user");
		PageBean pageBean = null;
		Map<String, Object> map = new HashMap<String, Object>();
		if (page_num == null) {
			page_num = "1";
		}
		if(user.getRole()!=1)
		{
			map.put("user_id",user_id);
		}
		update_jobitem_call();
		try {
			pageBean =callService.search_jobitem_call(map, super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
	        logger.error("查询打电话任务失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("查询打电话任务失败", e);
		}
		model.addAttribute("user_id",user_id);
		model.addAttribute("jobitem_calls", pageBean.getList());
		model.addAttribute("page", pageBean);
		return "/jobitem_call/add_jobitem_call";
	}
	//添加满足条件的信息到jonitem_call表中
	@RequestMapping(value = "/add_jobitem_call.do")
	public String add_jobitem_call(String user_id)
	{
		Adminuser user=null;
	//	Jobitem_call jobitem_call=new Jobitem_call();																																						
		Jobitem_User jobitem_user;
		List<Jobitem_User> list=new ArrayList<Jobitem_User>();
		try {
			user=userService.getbyId(Integer.parseInt(user_id));
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
            logger.error("获取admin用户失败", e1);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			 logger.error("获取admin用户失败", e1);
		}
		if(user.getRole()!=1)
		{
		try {
			list=(List<Jobitem_User>)callService.search_add_jobitem_calls();
			for(int i=0;i<list.size();i++)
			{
				Jobitem_call jobitem_call=new Jobitem_call();
				jobitem_user=list.get(i);
			//	if(jobitem_user.getJobitem().getJob().getArea().getCode()==user.getCounty().getCode())
			//	{
				jobitem_call.setAdminuser(user);
				jobitem_call.setJobitem(jobitem_user.getJobitem());
				jobitem_call.setStatus(1);
				if(jobitem_user.getJobitem().getJobitem_status()==2)
				{
					jobitem_call.setJobitem_type(1);
				}
				else if(jobitem_user.getJobitem().getJobitem_status()==5 && jobitem_user.getRework_status()==0)
				{
					jobitem_call.setJobitem_type(2);
				}
				else if(jobitem_user.getRework_status()==1 && jobitem_user.getJobitem().getJobitem_status()==5)
				{
					jobitem_call.setJobitem_type(3);
				}
				callService.add_jobitem_call(jobitem_call);
			}
			//}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("查找符合条件的数据失败", e);
		}
		}
		return "redirect:/show_add_jobitem_call.do?user_id="+user_id+"";
	}
	//获取单个记录的具体的信息
	@RequestMapping(value = "/get_jobitem_call.do")
	public String get_jobitem_call(String id,Model model)
	{	User userboss;
		Jobitem_call jobitem_call;
		
		try {
			callService.update_jobitem_callstatus(Integer.parseInt(id));
			jobitem_call=callService.get_jobitem_calls(Integer.parseInt(id));
			
			List<Jobitem_User> list;
			List<Jobitem_User> reallist=new ArrayList<Jobitem_User>();
			List<Jobitem_User> alllist=new ArrayList<Jobitem_User>();
			List<Jobitem_User> reworklist=new ArrayList<Jobitem_User>();
			Jobitem_User jobitem_user;
			if(jobitem_call.getJobitem_type()==1)
			{
				list=jobService.getbyjobitem_id_jobitem_user(jobitem_call.getJobitem().getId());
				userboss=jobitem_call.getJobitem().getJob().getUser();
				model.addAttribute("readylist",userboss);
				for(int i=0;i<list.size();i++)
				{
					jobitem_user=list.get(i);
					if(jobitem_user.getJobitem_status()==0 && jobitem_user.getRework_status()==0 && jobitem_user.getStatus()==1)
					{
						alllist.add(jobitem_user);
					}
				}
				model.addAttribute("alllist",alllist);
			}
			if(jobitem_call.getJobitem_type()==2)
			{
				list=jobService.getbyjobitem_id_jobitem_user(jobitem_call.getJobitem().getId());
				for(int i=0;i<list.size();i++)
				{
					jobitem_user=list.get(i);
					if(jobitem_user.getJobitem_status()==1 && jobitem_user.getRework_status()==0 && jobitem_user.getStatus()==1)
					{
						reallist.add(jobitem_user);
					}
				}
				model.addAttribute("userlist",reallist);
			
			}
			if(jobitem_call.getJobitem_type()==3)
			{
				list=jobService.getbyjobitem_id_jobitem_user(jobitem_call.getJobitem().getId());
				for(int i=0;i<list.size();i++)
				{
					jobitem_user=list.get(i);
					if(jobitem_user.getRework_status()==1 && jobitem_user.getStatus()==1 && jobitem_user.getJobitem_status()==1)
					{
						reworklist.add(jobitem_user);
					}
				}
				model.addAttribute("reworklist",reworklist);
			
			}
			
			model.addAttribute("jobitem_call", jobitem_call);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
		logger.error("获取单个call记录失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取单个call记录失败", e);
		}
		return "/jobitem_call/jobitem_calldetail";
	}
	//前台某工作接单人全部退单修改jobitem_call的状态
	public void update_jobitem_call()
	{
		List<Jobitem_call> list=new ArrayList<Jobitem_call>();
		Jobitem_call jobitem_call=null;
		try {
			list=callService.search_alljobitem_call();
			for(int i=0;i<list.size();i++)
			{
			jobitem_call=list.get(i);
			callService.delete_jobitem_call(jobitem_call.getJobitem().getId());			
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
	        logger.error("查询全部打电话任务失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("查询全部打电话任务失败", e);
		}

	}
}
